﻿/*
 * Copyright (C) 2012 TheLionsRoar <thelionwillalwaysroar@gmail.com>
 * Copyright (C) 2012 Jedrix <jedrix2454@gmail.com>
 * 
 * This file is part of the LEO Game Library.
 * 
 * LEO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * LEO is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have receieved a copy of the GNU General Public License
 * along with LEO. If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;

namespace LEO.Framework {
    public class Camera : Entity {
        private float zoom;
        private Matrix transform;
        private float rotation;
        private Rectangle viewport;
        private AudioListener audioListener;
        private static Camera currentCamera;

        public Camera(int width, int height) {
            Zoom = 1.0F;
            Rotation = 0.0F;
            Position = Vector2.Zero;
            Viewport = new Rectangle(0, 0, width, height);

            AudioListener = new AudioListener();
            AudioListener.Position = new Vector3(Position.X, 0, Position.Y);
        }

        public float Zoom {
            get { return zoom; }
            set { zoom = value; if(zoom < 0.1F) zoom = 0.1F; }
        }

        public float Rotation {
            get { return rotation; }
            set { rotation = value; }
        }

        public Rectangle Viewport {
            get { return viewport; }
            set { viewport = value; }
        }

        public AudioListener AudioListener {
            get { return audioListener; }
            set { audioListener = value; }
        }

        public static Camera CurrentCamera {
            get { return currentCamera; }
            set { currentCamera = value; }
        }

        public void Move(Vector2 amount) {
            Position += amount;
            AudioListener.Position = new Vector3(Position.X, 0, Position.Y);
        }

        public void Follow(Entity target) {
            Position = target.Position;
            AudioListener.Position = new Vector3(Position.X, 0, Position.Y);
        }

        public Matrix GetTransformation(GraphicsDevice graphicsDevice) {
            transform = Matrix.Identity *
                Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) *
                Matrix.CreateRotationZ(Rotation) *
                Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                Matrix.CreateTranslation(new Vector3(Viewport.Width * 0.5F, Viewport.Height * 0.5F, 0));
            return transform;
        }

        public Vector2 ToWorldLocation(Vector2 position) {
            return Vector2.Transform(position, Matrix.Invert(transform));
        }

        public Vector2 ToLocalLocation(Vector2 position) {
            return Vector2.Transform(position, transform);
        }
    }
}
